package com.example.tanlee.ocrgraduation.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.tanlee.ocrgraduation.application.MyApplication;

/**
 * 关于业务逻辑的util类
 * Created by tanlee on 2015/10/19.
 */
public class SharedPreferenceUtil {

    /*
    * SharePreference
    */
    // SharedPreference中 文件名
    public static final String USER_FILE = "User_file";
    // SharedPreference中 记录以后是否自动登录，登录时保存为true，注销时保存为false
    public static final String AUTOLOGINLATER = "auto_login_later";
    // SharedPreference中 是否第一次打开软件
    public static final String FIRST_STARTE = "first_start";
    // SharedPreference中 是否第一次打开需求
    public static final String FIRST_DEMAND = "first_demand";
    // SharedPreference中 是否第一次打开HomeActivity
    public static final String FIRST_HOMEPAGE_TIMES = "first_homepage_times";
    // SharedPreference中 是否第一次打开IteachActivity
    public static final String FIRST_ITEACH = "first_iteach";
    // SharedPreference中 记录老版本的版本号，用于升级时记录被替换的版本的版本号
    public static final String OLD_VERSION_CODE = "old_version_code";
    // SharedPreference中 记录消息条数
//    public static final String NEW_INFO_NUM = "new_info_num";
    // SharedPreference中存手机号帐号
    public static final String PHONE = "phone";
    // SharedPreference中存手机号
    public static final String CROWDFUND_PHONE = "crowd_fund_phone";
    // SharedPreference中存密码(md5)
    public static final String PASSWORD = "password";
    // SharedPreference中存用户数据
    public static final String SUSER = "suser";
    // SharedPreference中存所有科目信息
    public static final String ALL_SUBJECT_MAP = "all_subject_map";
    // SharePreference中存所有科目信息
    public static final String ALL_SUBJECT_LISt = "all_subject_list";
    // 用户所选的科目信息
    public static final String USER_SUBJECT = "user_subject";
    // 群聊跳转
    public static final String CHAT_INTENT = "chat_intent";

    //记录用户选择的频道，以便下次打开应用时默认显示他退出时所在的频道
    public static final String HOME_DEMAND_CHENNEL_SELECTED = "home_demand_channel_selected";
    public static final String HOME_AACOURSE_CHANNEL_SELECTED = "home_aacourse_channel_selected";
    public static final String HOME_COURSE_CHANNEL_SELECTED = "home_course_channel_selected";
    // 我要讲课 存在SharedPreference中的数据
    private static final String ITEACH_AACOURSE = "iteach_aacourse";
    // 我要听课
    public  static final String ILEARN = "ilearn";
    private static final String USER_CHANNEL = "user_channel";
    private static final String USER_CHANNEL_ID = "user_channel_id";
    public static final String NEED_TO_DOWNLOAD_BAIDU_SO = "need_to_download_baidu_so";
    public static final String CLIENT_VERSION = "client_version";

    public static String shortUrl = "null";

    private static class SharedPreferencesHolder {
        private static SharedPreferences setting = MyApplication.getInstance().getSharedPreferences(
                USER_FILE, Context.MODE_MULTI_PROCESS);
    }

    public static final SharedPreferences getSharedPreferencesInstance() {
        return SharedPreferencesHolder.setting;
    }

    public static final SharedPreferences.Editor getEditorInstance() {
        return getSharedPreferencesInstance().edit();
    }

    public static int getSharePreInt(String itemTag, int defval) {
        return getSharedPreferencesInstance().getInt(itemTag, defval);
    }

    public static boolean putSharePreInt(String itemTag,
                                         int val) {
        SharedPreferences.Editor editor = getEditorInstance();
        editor.putInt(itemTag, val);
        return editor.commit();
    }

    public static String getSharePreString(String itemTag,
                                           String defValue) {
        return getSharedPreferencesInstance().getString(itemTag, defValue);
    }

    public static boolean getSharePreBoolean(String itemTag,
                                             boolean defValue) {
        return getSharedPreferencesInstance().getBoolean(itemTag, defValue);
    }

    public static boolean putSharePreString(String itemTag,
                                            String val) {
        SharedPreferences.Editor editor = getEditorInstance();
        editor.putString(itemTag, val);
        return editor.commit();
    }

//    public static boolean putSharePreStringList(String key,
//                                                List<String> list) {
//        SharedPreferences.Editor editor = getEditorInstance();
//        Gson gson = new Gson();
//        editor.putString(key,gson.toJson(list));
//        return editor.commit();
//    }

//    public static ArrayList<String> getSharePreStringList(String key) {
//        ArrayList<String> list = JSON.parseObject(getSharedPreferencesInstance().getString(key, null),
//                new TypeReference<ArrayList<String>>() {
//                });
//        if (list == null)
//            return new ArrayList<>();
//        return list;
//    }

    public static boolean putSharePreBoolean(String itemTag,
                                             boolean val) {
        SharedPreferences.Editor editor = getEditorInstance();
        editor.putBoolean(itemTag, val);
        return editor.commit();
    }

    /**
     * 保存对象
     */
//    public static boolean putObject(String tag, Object obj) {
//        SharedPreferences.Editor editor = getEditorInstance();
//        editor.putString(tag, JSON.toJSONString(obj));
//        return editor.commit();
//    }

    /**
     * 删除对象
     */
    public static boolean removeObject(String tag) {
        SharedPreferences.Editor editor = getEditorInstance();
        editor.remove(tag);
        return editor.commit();
    }

//    /**
//     * 获取版本信息
//     */
//    public static SClientVersion getSClientVersion() {
//        return JSON.parseObject(
//                getSharedPreferencesInstance().getString(CLIENT_VERSION,
//                        JSON.toJSONString(new SClientVersion())),
//                new TypeReference<SClientVersion>() {
//                });
//    }

    /**
     * 保存全部科目信息
     *
     * @param subjectMap
     * @return
     */
//    public static boolean putALLSubjectMap(HashMap<Integer, String> subjectMap) {
//        SharedPreferences.Editor editor = getEditorInstance();
//        editor.putString(ALL_SUBJECT_MAP, JSON.toJSONString(subjectMap));
//        return editor.commit();
//    }

    /**
     * 获取全部科目信息
     *
     * @return
     */
//    public static HashMap<Integer, String> getAllSubjectMap() {
//        String preString = getSharedPreferencesInstance()
//                .getString(ALL_SUBJECT_MAP, JSON.toJSONString(new HashMap<Integer, String>()));
//        return JSON.parseObject(preString,
//                new TypeReference<HashMap<Integer, String>>() {
//                });
//    }

}
